//package no.group.gowther.service;
//
//import no.group.gowther.dao.es.ElasticCompressPasswordDao;
//import no.group.gowther.exception.BusinessException;
//import no.group.gowther.exception.ErrorMessage;
//import no.group.gowther.exception.UncompressFailureException;
//import no.group.gowther.service.dto.ListDTO;
//import no.group.gowther.service.dto.RealFilePath;
//import no.group.gowther.dao.entity.CompressPwd;
//import no.group.gowther.rest.vo.param.AddCompressPwdParam;
//import no.group.gowther.rest.vo.param.QueryCompressPwdParam;
//import no.group.gowther.rest.vo.param.UncompressFileParam;
//import no.group.gowther.util.FilePathChecker;
//import no.group.gowther.util.FileUtils;
//import no.group.gowther.util.UncompressFileUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//
//import java.io.IOException;
//import java.nio.file.Files;
//import java.nio.file.Path;
//
///**
// * @author ayatsuji
// * @date 2022-04-12 23:06
// * @since
// */
//@Service
//public class CompressFileServiceImpl implements CompressFileService {
//    private static final Logger LOGGER = LoggerFactory.getLogger(CompressFileServiceImpl.class);
//
//    private final FilePathMappingService filePathMappingService;
//    private final ElasticCompressPasswordDao compressPasswordDao;
//
//    public CompressFileServiceImpl(FilePathMappingService filePathMappingService, ElasticCompressPasswordDao compressPasswordDao) {
//        this.filePathMappingService = filePathMappingService;
//        this.compressPasswordDao = compressPasswordDao;
//    }
//
//    @Value("${uncompress-min-usable-space-rate:2}")
//    private int uncompressMinUsableSpaceRate = 2;
//
//    @Override
//    public void preCheckUncompress(UncompressFileParam preCheckParam) {
//        final RealFilePath compressFilePath = filePathMappingService.toRealPath(preCheckParam.getFilePath());
//        FilePathChecker.checkFileExist(preCheckParam.getFilePath(), compressFilePath);
//        final RealFilePath targetDirPath = filePathMappingService.toRealPath(preCheckParam.getTargetDirPath());
//        FilePathChecker.checkDirExist(preCheckParam.getTargetDirPath(), targetDirPath);
//        try {
//            final long compressFileSize = Files.size(compressFilePath.toPath());
//            final long usableSpace = FileUtils.getUsableSpace(targetDirPath.toPath());
//            if (usableSpace < compressFileSize * uncompressMinUsableSpaceRate) {
//                throw BusinessException.createException(ErrorMessage.PARAM_ERROR.getErrorCode(), String.format("无法解压[%s]，目标路径[%s]空间不足", preCheckParam.getFilePath(), preCheckParam.getTargetDirPath()));
//            }
//        } catch (IOException e) {
//            LOGGER.error("计算路径磁盘空间[{}]和文件[{}]大小失败", preCheckParam.getTargetDirPath(), preCheckParam.getFilePath(), e);
//            throw BusinessException.createException(ErrorMessage.SYSTEM_ERROR.getErrorCode(), ErrorMessage.SYSTEM_ERROR.getErrorMsg());
//        }
//    }
//
//    @Override
//    public void uncompress(UncompressFileParam uncompressFileParam) {
//        final RealFilePath compressFilePath = filePathMappingService.toRealPath(uncompressFileParam.getFilePath());
//        FilePathChecker.checkFileExist(uncompressFileParam.getFilePath(), compressFilePath);
//        final RealFilePath targetDirPath = filePathMappingService.toRealPath(uncompressFileParam.getTargetDirPath());
//        final Path targetDir = targetDirPath.concat(compressFilePath.getFilenameWithoutExtension()).toPath();
//        for (String pwd: uncompressFileParam.getPwd()) {
//            if (!compressPasswordDao.existPwd(pwd)) {
//                CompressPwd compressPwd = new CompressPwd();
//                compressPwd.setPwd(pwd);
//                compressPasswordDao.addPwd(compressPwd);
//            }
//            try {
//                final boolean compressSuccess = UncompressFileUtils.uncompress(compressFilePath.toPath(), targetDir, pwd);
//                if (compressSuccess) {
//                    final CompressPwd compressPwd = compressPasswordDao.queryOneEqPwd(pwd);
//                    compressPwd.setMatchNum(compressPwd.getMatchNum() + 1);
//                    compressPasswordDao.updatePwd(compressPwd);
//                    return;
//                }
//            } catch (UncompressFailureException e) {
//                LOGGER.info("解压文件[{}]到[{}]失败", compressFilePath, targetDir, e);
//                throw BusinessException.createException(ErrorMessage.PARAM_ERROR.getErrorCode(), String.format("未知错误导致解压文件[%s]失败", uncompressFileParam.getFilePath()));
//            }
//        }
//    }
//
//    @Override
//    public ListDTO<CompressPwd> queryCompressPwd(QueryCompressPwdParam queryCompressPwdParam) {
//        return compressPasswordDao.queryListByFuzzilyPwd(queryCompressPwdParam);
//    }
//
//    @Override
//    public void deleteCompressPwd(String pwdId) {
//        compressPasswordDao.deletePwd(pwdId);
//    }
//
//    @Override
//    public void addCompressPwd(AddCompressPwdParam addCompressPwdParam) {
//        if (compressPasswordDao.existPwd(addCompressPwdParam.getPwd())) {
//            throw BusinessException.createException(ErrorMessage.PARAM_ERROR.getErrorCode(), "压缩密码已存在");
//        }
//        CompressPwd compressPwd = new CompressPwd();
//        compressPwd.setPwd(addCompressPwdParam.getPwd());
//        compressPasswordDao.addPwd(compressPwd);
//    }
//
//    public void setUncompressMinUsableSpaceRate(int uncompressMinUsableSpaceRate) {
//        this.uncompressMinUsableSpaceRate = uncompressMinUsableSpaceRate;
//    }
//}
